<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

/**
 * Manager Controller
 *
 * @package     VisualQ Local System
 * @author      CSU OJT Students '10 - '11 (Michael Roland B. Martin)
 * @copyright   Copyright (c) 2010 - 2011, Bitstop Network Services, Inc.
 * @version     1.0 January 27, 2011
 */

class Manager extends Controller
{

    function Manager()
    {
        parent::Controller();

        $this->load->helper('cookie');
        $this->lang->load('local', 'English');

        // Check if user is logged in
        if($this->session->userdata('id'))
        {
            if(strcmp($this->session->userdata('account_type'), 'agent') == 0)
            {
                redirect(base_url() . 'agent/');
            }
        }
        else
        {
            redirect(base_url() . 'login/');
        }

        // Check if the front desk or display cookie is on
        if(get_cookie('unit'))
        {
            redirect(base_url() . 'unit/');
        }

        //$this->output->enable_profiler(TRUE);
    }

    function index()
    {
        if(strcmp($this->session->userdata('account_type'), 'agent') == 0)
        {
            redirect(base_url() . 'agent/');
        }

        redirect(base_url() . 'manager/statistics');
    }

    function statistics()
    {
        $this->load->model(array('statistics_model', 'service_model', 'account_model'));

        $result = $this->service_model->get_available_services();

        $total_served    = 0;
        $total_missed    = 0;
        $total_waiting   = 0;
        $total_processing_time   = 0;

        foreach($result->result_array() as $row)
        {
            unset($closing_codes_tally);

            // Get service closing codes
            $closing_codes = json_decode($row['closing_codes']);

            $params['si'] = $row['service_id'];

            $frequency_total = 0;
            foreach($closing_codes as $closing_code_row)
            {
                // Get closing code statistics
                $params['closing_code'] = $closing_code_row;
                $result = $this->statistics_model->get_closing_code_tally($params);

                if($result->num_rows() > 0)
                {
                    $frequency = 0;
                    foreach($result->result_array() as $tally)
                    {
                        $frequency          += $tally['frequency'];
                    }

                    $closing_codes_tally[] = array(
                        'name'      => $params['closing_code'],
                        'frequency' => $frequency
                    );

                    $frequency_total    += $frequency;
                }
            }

            $closing_codes_tally[] = array(
                'name'      => 'total',
                'frequency' => $frequency_total
            );

            // Get service statistics
            $params['type'] = 'served';
            $served = $this->statistics_model->get_service_statistics($params);
            $params['type'] = 'missed';
            $missed = $this->statistics_model->get_service_statistics($params);
            $params['type'] = 'waiting';
            $waiting = $this->statistics_model->get_service_statistics($params);

            $result = $this->statistics_model->get_service_logs($params['si']);

            $processing_time = 0;
            foreach($result->result_array() as $pt)
            {
                $processing_time        += $pt['processing_time'];
                $total_processing_time  += $pt['processing_time'];
            }

            $stat = array(
                'title'             => $row['title'],
                'served'            => $served,
                'missed'            => $missed,
                'waiting'           => $waiting,
                'processing_time'   => $processing_time / ($served == 0 ? 1 : $served),
                'closing_codes'     => $closing_codes_tally
            );

            // Increment totals
            $total_served    += $served;
            $total_missed    += $missed;
            $total_waiting   += $waiting;

            // Prepare the statistics
            $data['services_stat']['individual'][] = $stat;
        }

        // Set totals
        $data['services_stat']['total_served']   = $total_served;
        $data['services_stat']['total_missed']   = $total_missed;
        $data['services_stat']['total_waiting']  = $total_waiting;
        $data['services_stat']['total_protime']  = $total_processing_time / ($total_served == 0 ? 1 : $total_served);

        // Get agents
        $result = $this->account_model->get_agents_details();

        $total_served    = 0;
        $total_missed    = 0;
        $total_processing_time   = 0;
        
        foreach($result->result_array() as $row)
        {
            // Get served statistic
            $params = array(
                'ai'    => $row['id'],
                'type'  => 'served'
            );

            $result = $this->statistics_model->get_agent_logs($params);

            $served = $result->num_rows();
            $total_served    += $served;

            $processing_time = 0;
            if($served > 0)
            {
                foreach($result->result_array() as $pt)
                {
                    $processing_time += $pt['processing_time'];
                    $total_processing_time += $pt['processing_time'];
                }
            }

            $params['type'] = 'missed';
            $result = $this->statistics_model->get_agent_logs($params);

            $missed = $result->num_rows();
            $total_missed    += $missed;

            $stat = array(
                'name'              => $row['last_name'] . ', ' . $row['first_name'],
                'served'            => $served,
                'missed'            => $missed,
                'processing_time'   => $processing_time / ($served == 0 ? 1 : $served)
            );
            
            // Prepare the statistics
            $data['agents_stat']['individual'][] = $stat;
        }

        // Set totals
        $data['agents_stat']['total_served']   = $total_served;
        $data['agents_stat']['total_missed']   = $total_missed;
        $data['agents_stat']['total_protime']  = $total_processing_time / ($total_served == 0 ? 1 : $total_served);

        $this->load->view('manager_statistics_view', $data);
    }

    /**
     * Build the agents list flexigrid
     *
     * NOTE: The method is based on the codeigniter implementation prototype
     */
    function agents_grid()
    {
        $this->load->helper('flexigrid');

        // Set the columns and column properties
        $colModel = array(
            'id'            => array($this->lang->line('manager_id'),          40,     TRUE,   'center',   2),
            'last_name'     => array($this->lang->line('manager_last_name'),   140,    TRUE,   'left',     1),
            'first_name'    => array($this->lang->line('manager_first_name'),  140,    TRUE,   'left',     1),
            'services'      => array($this->lang->line('manager_services'),    320,    TRUE,   'left',     0),
            'actions'       => array($this->lang->line('manager_actions'),     40,     TRUE,   'left',     0)
        );

        // Set the flexigrid properties
        $gridParams = array(
            'width'                 => 760,
            'height'                => 300,
            'rp'                    => 15,
            'rpOptions'             => '[10,15,20,25,40]',
            'pagestat'              => 'Displaying: {from} to {to} of {total} items.',
            'blockOpacity'          => 0.5,
            'showTableToggleBtn'    => FALSE
        );

        // Build flexigrid js
        $grid_js = build_grid_js('flexigrid', site_url("ajax/agents_grid/"), $colModel, 'id', 'asc', $gridParams);
        $data['js_grid'] = $grid_js;

        $this->load->view('agent_list_view', $data);
    }

    /**
     * Show agent information
     *
     * @param	array
     *      id      :   account id
     */
    function account_info($id)
    {
        $this->load->model('account_model');

        $result = $this->account_model->get_account($id);

        if($result->num_rows() > 0)
        {
            $data['account'] = $result->row_array();

            // Get agent services
            $this->load->model('service_model');
            $result = $this->service_model->get_agent_services($id);

            $data['services'] = $result->result_array();

            // Get country
            $result = $this->account_model->get_country($data['account']['country_id']);
            $data['country'] = $result->row_array();

            $data['valid'] = TRUE;
        }
        else
        {
            $data['valid'] = FALSE;
        }

        $this->load->view('account_info_view', $data);
    }

    /**
     * Show deactivate system registration
     */
    function deactivate()
    {
        $this->load->view('deactivate_view');
    }

    function unit()
    {
        // Check if unit is registered
        if($this->session->userdata('unit_name'))
        {
            $data['show'] = 0;

            // If front desk or display, show the activate button
            if(strcmp($this->session->userdata('unit_type'), 'Front Desk') == 0 || strcmp($this->session->userdata('unit_type'), 'Display') == 0)
            {
                $data['show'] = 1;
            }

            $this->load->view('unit_function_view', $data);
        }
        else
        {
            $this->load->view('setup_unit_view');
        }
    }

    /**
     * Deactivate unit registration
     */
    function deactivate_unit()
    {
        $this->session->unset_userdata('unit_name');
        $this->session->unset_userdata('unit_type');

        redirect(base_url() . 'manager/unit/');
    }

    function register_unit()
    {
        // Generate unit name
        $this->load->model('unit_model');
        $result = $this->unit_model->get_unit_type_count($this->input->post('unit_type'));

        // Register
        $params = array(
            'unit_name' => $this->input->post('unit_type') . ' ' . ($result->num_rows() + 1),
            'unit_type' => $this->input->post('unit_type')
        );
        $this->unit_model->register($params);

        redirect(base_url() . 'manager/unit/');
    }

}

/* End of file manager.php */
/* Location: ./system/application/controllers/manager.php */